package com.chao.demo.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * WebDriver做公司自动冻结操作
 * @author 超人不会飞
 * @date 2023-5-26
 */
@Api(value = "WebDriver测试API", tags = {"WebDriver测试API"})
@RestController
public class WebDriverScriptTest {

    private static String getWebUrl = "https://d.baiwang.com/channelmanage/manageLogin";
    private static String userName = "nzyh1700000054052";
    private static String passWord = "Aa123456";
    private static String company = "优税猫";

    @ApiOperation(value = "测试开始", notes = "测试开始")
    @GetMapping("/get")
    public void webDriver() throws InterruptedException {
        // 环境配置
        System.setProperty("webdriver.chrome.driver", "/opt/soft/chromedriver");
        WebDriver driver;
        ChromeOptions option = new ChromeOptions();
        // 禁用沙盒
        option.addArguments("no-sandbox");

        // 浏览器配置
        driver = new ChromeDriver(option);
        driver.get(getWebUrl);// 获取的url地址
        driver.manage().window().maximize();// 最大窗口

        /**
         * 操作
         * 1、登录脚本
         * 2、查询脚本
         */
        // 登录
        WebElement element = driver.findElement(By.xpath("/html/body/div[1]/div/form/div[1]/div/div/input"));
        element.sendKeys(userName);
        element = driver.findElement(By.xpath("/html/body/div[1]/div/form/div[2]/div/div/input"));
        element.sendKeys(passWord);
        driver.findElement(By.xpath("/html/body/div[1]/div/form/button")).click();
        Thread.sleep(3000);

        // 查询
        // 点击在新窗口中打开的链接
        driver.navigate().refresh();
        driver.findElement(By.xpath("/html/body/div[1]/div/div[2]/div/div[1]/div/div/ul/li[3]/div")).click();
        Thread.sleep(1000);
        driver.findElement(By.xpath("/html/body/div[1]/div/div[2]/div/div[1]/div/div/ul/li[3]/ul/li[3]/div")).click();
        Thread.sleep(1000);
        driver.findElement(By.xpath("/html/body/div[1]/div/div[2]/div/div[1]/div/div/ul/li[3]/ul/li[3]/ul/li[2]")).click();
        Thread.sleep(2000);

        driver.switchTo().frame("incomeInterface");
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[1]/span/input[1]")).sendKeys(company);
        driver.findElement(By.id("fp_btn_query")).click();

        Thread.sleep(2000);
        System.out.println("我"+driver.findElement(By.xpath("/html/body/div[1]/div[2]/div/div/div[1]/div[2]/div[2]/table/tbody/tr/td[3]/div")).getText());
        driver.findElement(By.xpath("/html/body/div[1]/div[2]/div/div/div[1]/div[2]/div[2]/table/tbody/tr/td[20]/div/a[3]")).click();

        Thread.sleep(2000);
        driver.findElement(By.id("citySel")).sendKeys("四川业翊贸易有限公司");
        driver.findElement(By.xpath("/html/body/div[2]/div/div[3]/div[1]/div/a")).click();

        // 点击冻结
    }

    public static void main(String[] args) throws InterruptedException {
        // 环境配置
        System.setProperty("webdriver.chrome.driver", "D:\\chorme_down\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver;
        ChromeOptions option = new ChromeOptions();
        // 禁用沙盒
        option.addArguments("no-sandbox");

        // 浏览器配置
        driver = new ChromeDriver(option);
        driver.get(getWebUrl);// 获取的url地址
        driver.manage().window().maximize();// 最大窗口

        /**
         * 操作
         * 1、登录脚本
         * 2、查询脚本
         */
        // 登录
        WebElement element = driver.findElement(By.xpath("/html/body/div[1]/div/form/div[1]/div/div/input"));
        element.sendKeys(userName);
        element = driver.findElement(By.xpath("/html/body/div[1]/div/form/div[2]/div/div/input"));
        element.sendKeys(passWord);
        driver.findElement(By.xpath("/html/body/div[1]/div/form/button")).click();
        Thread.sleep(3000);
    }
}
